package com.example.archermind.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AndroidControlActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.btn_androidcontrolbinder)
    Button btnAndroidcontrolbinder;
    @BindView(R.id.btn_setStartupState)
    Button btnSetStartupState;
    @BindView(R.id.btn_switchToAndroid)
    Button btnRequestFullScreen;
    @BindView(R.id.btn_changeStatusBar)
    Button btnChangeStatusBar;
    @BindView(R.id.et_setStartupState)
    EditText etSetStartupState;
    @BindView(R.id.et_changeStatusBar)
    EditText etChangeStatusBar;
    @BindView(R.id.tv_setStartupState)
    TextView tvSetStartupState;
    @BindView(R.id.tv_changeStatusBar)
    TextView tvChangeStatusBar;
    @BindView(R.id.tv_systemswitch)
    TextView tvSystemSwitch;
    @BindView(R.id.et_dialPhoneNumber)
    EditText etDialPhoneNumber;
    @BindView(R.id.btn_dialPhoneNumber)
    Button btnDialPhoneNumber;
    @BindView(R.id.tv_dialPhoneNumber)
    TextView tvDialPhoneNumber;
    @BindView(R.id.btn_getCurrentHMISystem)
    Button btnGetCurrentHMISystem;
    @BindView(R.id.tv_getCurrentHMISystem)
    TextView tvGetCurrentHMISystem;
    @BindView(R.id.et_switchToAndroid)
    EditText etSwitchToAndroid;
    @BindView(R.id.et_switchToLinux)
    EditText etSwitchToLinux;
    @BindView(R.id.btn_switchToLinux)
    Button btnSwitchToLinux;
    @BindView(R.id.tv_switchToLinux)
    TextView tvSwitchToLinux;
    @BindView(R.id.btn_registerSystemSwitchCallback)
    Button btnRegisterSystemSwitchCallback;
    @BindView(R.id.btn_unregisterSystemSwitchCallback)
    Button btnUnregisterSystemSwitchCallback;


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            int status = bundle.getInt("mode");
            int reslut = bundle.getInt("reslut");
            int system = bundle.getInt("system");
            switch (msg.what) {
                case CHANGESTATUSBARCALLBACK + 0:
                    tvChangeStatusBar.setText("ChangeStatusBar status" + status + "");
                    break;
                case SYSTEMSWITCHCALLBACK + 0:
                    tvSystemSwitch.setText("SystemSwitch system:" + system + "");
                    break;
                case DIALPHONENUMBERBARCALLBACK + 0:
                    tvDialPhoneNumber.setText("DialPhoneNumber reslut:" + reslut + "");
                    break;

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_control);
        unbinder = ButterKnife.bind(this);
        setHandler(mHandler);
    }


    @Override
    @OnClick({R.id.btn_androidcontrolbinder, R.id.btn_setStartupState, R.id.btn_changeStatusBar,
            R.id.btn_dialPhoneNumber, R.id.btn_getCurrentHMISystem,
            R.id.btn_switchToAndroid, R.id.btn_switchToLinux, R.id.btn_registerSystemSwitchCallback, R.id.btn_unregisterSystemSwitchCallback

    })
    public void onClick(View view) {
        try {
            switch (view.getId()) {
                case R.id.btn_androidcontrolbinder:
                    getAndroidControlService();
                    break;
                case R.id.btn_setStartupState:
                    if (TextUtils.isEmpty(etSetStartupState.getText())) {
                        Toast.makeText(this, "请输入参数", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int state = Integer.parseInt(etSetStartupState.getText().toString());
                    getAndroidControlService().setStartupState(state);
                    break;
                case R.id.btn_switchToAndroid:
                    if (TextUtils.isEmpty(etSwitchToAndroid.getText().toString())) {
                        Toast.makeText(this, "请输入参数", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int androidView = Integer.parseInt(etSwitchToAndroid.getText().toString());
                    getAndroidControlService().switchToAndroid(androidView);
                    break;
                case R.id.btn_switchToLinux:
                    if (TextUtils.isEmpty(etSwitchToLinux.getText().toString())) {
                        Toast.makeText(this, "请输入参数", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int linuxView = Integer.parseInt(etSwitchToLinux.getText().toString());
                    getAndroidControlService().switchToLinux(linuxView);
                    break;
                case R.id.btn_registerSystemSwitchCallback:
                    getAndroidControlService().registerSystemSwitchCallback(getSystemSwitchCallback());
                    break;
                case R.id.btn_unregisterSystemSwitchCallback:
                    getAndroidControlService().unregisterSystemSwitchCallback(getSystemSwitchCallback());
                    break;
                case R.id.btn_changeStatusBar:
                    if (TextUtils.isEmpty(etChangeStatusBar.getText())) {
                        Toast.makeText(this, "请输入参数", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    state = Integer.parseInt(etChangeStatusBar.getText().toString());
                    getAndroidControlService().changeStatusBar(state, getChangeStatusBarCallback());
                    break;
                case R.id.btn_getCurrentHMISystem:
                    int hmi = getAndroidControlService().getCurrentHMISystem();
                    tvGetCurrentHMISystem.setText(hmi+"");
                    break;
                case R.id.btn_dialPhoneNumber:
                    if (TextUtils.isEmpty(etDialPhoneNumber.getText())) {
                        Toast.makeText(this, "请输入参数", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    getAndroidControlService().dialPhoneNumber(etDialPhoneNumber.getText().toString(), getDialPhoneNumberBarCallback());
                    break;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
